package org.gdstash.ui.character;

import org.gdstash.character.GDChar;
import org.gdstash.character.GDCharTeleportList;
import org.gdstash.character.GDCharUID;
import org.gdstash.ui.GDStashFrame;
import org.gdstash.ui.util.GDAbstractRiftPane;
import org.gdstash.util.GDMsgFormatter;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import java.util.List;

public class RoTRiftAct5Pane extends GDAbstractRiftPane {
    private JCheckBox cbHarrogath;
    private JCheckBox cbFrigidHighlands;
    private JCheckBox cbArreatPlateau;
    private JCheckBox cbCrystallinePassage;
    private JCheckBox cbHallsPain;
    private JCheckBox cbGlacialTrail;
    private JCheckBox cbFrozenTundra;
    private JCheckBox cbAncientsWay;
    private JCheckBox cbWorldstoneKeep;
    private int difficulty;
    private boolean changed;

    public RoTRiftAct5Pane(final int difficulty, final int direction) {
        this.difficulty = difficulty;
        this.changed = false;
        this.adjustUI();
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(this.cbHarrogath).addComponent(this.cbFrigidHighlands).addComponent(this.cbArreatPlateau).addComponent(this.cbCrystallinePassage).addComponent(this.cbHallsPain).addComponent(this.cbGlacialTrail).addComponent(this.cbFrozenTundra).addComponent(this.cbAncientsWay).addComponent(this.cbWorldstoneKeep));
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(this.cbHarrogath)).addGroup(layout.createParallelGroup().addComponent(this.cbFrigidHighlands)).addGroup(layout.createParallelGroup().addComponent(this.cbArreatPlateau)).addGroup(layout.createParallelGroup().addComponent(this.cbCrystallinePassage)).addGroup(layout.createParallelGroup().addComponent(this.cbHallsPain)).addGroup(layout.createParallelGroup().addComponent(this.cbGlacialTrail)).addGroup(layout.createParallelGroup().addComponent(this.cbFrozenTundra)).addGroup(layout.createParallelGroup().addComponent(this.cbAncientsWay)).addGroup(layout.createParallelGroup().addComponent(this.cbWorldstoneKeep));
        if (direction == 0) {
            layout.setHorizontalGroup(vGroup);
            layout.setVerticalGroup(hGroup);
        } else {
            layout.setHorizontalGroup(hGroup);
            layout.setVerticalGroup(vGroup);
        }
        layout.linkSize(0, this.cbHarrogath, this.cbFrigidHighlands);
        layout.linkSize(0, this.cbHarrogath, this.cbArreatPlateau);
        layout.linkSize(0, this.cbHarrogath, this.cbCrystallinePassage);
        layout.linkSize(0, this.cbHarrogath, this.cbHallsPain);
        layout.linkSize(0, this.cbHarrogath, this.cbGlacialTrail);
        layout.linkSize(0, this.cbHarrogath, this.cbFrozenTundra);
        layout.linkSize(0, this.cbHarrogath, this.cbAncientsWay);
        layout.linkSize(0, this.cbHarrogath, this.cbWorldstoneKeep);
        layout.linkSize(1, this.cbHarrogath, this.cbFrigidHighlands);
        layout.linkSize(1, this.cbHarrogath, this.cbArreatPlateau);
        layout.linkSize(1, this.cbHarrogath, this.cbCrystallinePassage);
        layout.linkSize(1, this.cbHarrogath, this.cbHallsPain);
        layout.linkSize(1, this.cbHarrogath, this.cbGlacialTrail);
        layout.linkSize(1, this.cbHarrogath, this.cbFrozenTundra);
        layout.linkSize(1, this.cbHarrogath, this.cbAncientsWay);
        layout.linkSize(1, this.cbHarrogath, this.cbWorldstoneKeep);
    }

    @Override
    public void adjustUI() {
        Font fntLabel = UIManager.getDefaults().getFont("Label.font");
        Font fntCheck = UIManager.getDefaults().getFont("CheckBox.font");
        if (fntCheck == null) {
            fntCheck = fntLabel;
        }
        Font fntBorder = UIManager.getDefaults().getFont("TitledBorder.font");
        if (fntBorder == null) {
            fntBorder = fntLabel;
        }
        fntLabel = fntLabel.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntCheck = fntCheck.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntBorder = fntBorder.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        final Border lowered = BorderFactory.createEtchedBorder(1);
        final Border raised = BorderFactory.createEtchedBorder(0);
        final Border compound = BorderFactory.createCompoundBorder(raised, lowered);
        final TitledBorder text = BorderFactory.createTitledBorder(compound, GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_ACT5"));
        text.setTitleFont(fntBorder);
        this.setBorder(text);
        if (this.cbHarrogath == null) {
            (this.cbHarrogath = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbHarrogath.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_ROT_05_HARROGATH"));
        this.cbHarrogath.setFont(fntCheck);
        if (this.cbFrigidHighlands == null) {
            (this.cbFrigidHighlands = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbFrigidHighlands.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_ROT_05_FRIGID_HIGHLANDS"));
        this.cbFrigidHighlands.setFont(fntCheck);
        if (this.cbArreatPlateau == null) {
            (this.cbArreatPlateau = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbArreatPlateau.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_ROT_05_ARREAT_PLATEAU"));
        this.cbArreatPlateau.setFont(fntCheck);
        if (this.cbCrystallinePassage == null) {
            (this.cbCrystallinePassage = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbCrystallinePassage.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_ROT_05_CRYSTALLINE_PASSAGE"));
        this.cbCrystallinePassage.setFont(fntCheck);
        if (this.cbHallsPain == null) {
            (this.cbHallsPain = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbHallsPain.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_ROT_05_HALLS_PAIN"));
        this.cbHallsPain.setFont(fntCheck);
        if (this.cbGlacialTrail == null) {
            (this.cbGlacialTrail = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbGlacialTrail.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_ROT_05_GLACIAL_TRAIL"));
        this.cbGlacialTrail.setFont(fntCheck);
        if (this.cbFrozenTundra == null) {
            (this.cbFrozenTundra = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbFrozenTundra.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_ROT_05_FROZEN_TUNDRA"));
        this.cbFrozenTundra.setFont(fntCheck);
        if (this.cbAncientsWay == null) {
            (this.cbAncientsWay = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbAncientsWay.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_ROT_05_ANCIENTS_WAY"));
        this.cbAncientsWay.setFont(fntCheck);
        if (this.cbWorldstoneKeep == null) {
            (this.cbWorldstoneKeep = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbWorldstoneKeep.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_ROT_05_WORLDSTONE_KEEP"));
        this.cbWorldstoneKeep.setFont(fntCheck);
    }

    @Override
    public boolean hasChanged() {
        return this.changed;
    }

    @Override
    public void setChanged(final boolean changed) {
        this.changed = changed;
    }

    public void setSelected(final boolean selected) {
        this.cbHarrogath.setSelected(selected);
        this.cbFrigidHighlands.setSelected(selected);
        this.cbArreatPlateau.setSelected(selected);
        this.cbCrystallinePassage.setSelected(selected);
        this.cbHallsPain.setSelected(selected);
        this.cbGlacialTrail.setSelected(selected);
        this.cbFrozenTundra.setSelected(selected);
        this.cbAncientsWay.setSelected(selected);
        this.cbWorldstoneKeep.setSelected(selected);
    }

    @Override
    public List<GDCharUID> getRiftList(final boolean found) {
        final List<GDCharUID> list = new LinkedList<GDCharUID>();
        if (this.cbHarrogath.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_ROT_05_HARROGATH);
        }
        if (this.cbFrigidHighlands.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_ROT_05_FRIGID_HIGHLANDS);
        }
        if (this.cbArreatPlateau.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_ROT_05_ARREAT_PLATEAU);
        }
        if (this.cbCrystallinePassage.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_ROT_05_CRYSTALLINE_PASSAGE);
        }
        if (this.cbHallsPain.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_ROT_05_HALLS_PAIN);
        }
        if (this.cbGlacialTrail.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_ROT_05_GLACIAL_TRAIL);
        }
        if (this.cbFrozenTundra.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_ROT_05_FROZEN_TUNDRA);
        }
        if (this.cbAncientsWay.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_ROT_05_ANCIENTS_WAY);
        }
        if (this.cbWorldstoneKeep.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_ROT_05_WORLDSTONE_KEEP);
        }
        return list;
    }

    @Override
    public void setChar(final GDChar gdc) {
        this.setSelected(false);
        this.changed = false;
        if (gdc == null) {
            return;
        }
        final List<GDCharUID> list = gdc.getRiftList(this.difficulty);
        for (final GDCharUID uid : list) {
            if (uid.equals(GDCharTeleportList.UID_RIFT_ROT_05_HARROGATH)) {
                this.cbHarrogath.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_ROT_05_FRIGID_HIGHLANDS)) {
                this.cbFrigidHighlands.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_ROT_05_ARREAT_PLATEAU)) {
                this.cbArreatPlateau.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_ROT_05_CRYSTALLINE_PASSAGE)) {
                this.cbCrystallinePassage.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_ROT_05_HALLS_PAIN)) {
                this.cbHallsPain.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_ROT_05_GLACIAL_TRAIL)) {
                this.cbGlacialTrail.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_ROT_05_FROZEN_TUNDRA)) {
                this.cbFrozenTundra.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_ROT_05_ANCIENTS_WAY)) {
                this.cbAncientsWay.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_ROT_05_WORLDSTONE_KEEP)) {
                this.cbWorldstoneKeep.setSelected(true);
            }
        }
    }

    private class ChangeActionListener implements ActionListener {
        @Override
        public void actionPerformed(final ActionEvent e) {
            RoTRiftAct5Pane.this.changed = true;
        }
    }
}
